﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using BCCommon.Enums.Chats;
using BCDto.Chats.User;
using BCDto.Common.Logon;
using BCService;
using BCService.Chats;
using BCTMSRESTful.Configs;

namespace BCTMSRESTful.Controllers.Chats
{
    [ApiExplorerSettings(GroupName = "Chats")]
    [Route("api/v1/Chats/[controller]")]
    public class UsersController : ChatBaseController
    {
        private readonly static TimeSpan expiration = TimeSpan.FromDays(365);

        private readonly IWebSocketContextService webSocketContextService;
        private readonly IUserResolverService userResolverService;

        public UsersController(IWebSocketContextService webSocketContextService, IUserResolverService userResolverService)
        {
            this.webSocketContextService = webSocketContextService;
            this.userResolverService = userResolverService;
        }

        /// <summary>
        /// 获取指定聊天用户信息。
        /// </summary>
        /// <param name="chatUserId">聊天用户编号。</param>
        /// <response code="200">指定的聊天用户的用户信息。</response>
        /// <response code="204">指定的聊天用户不存在或已被删除。</response>
        /// <returns></returns>
        [HttpGet("{chat_user_id}")]
        [ProducesResponseType(typeof(UserDto), 200)]
        public IActionResult GetUser([FromRoute(Name = "chat_user_id")] string chatUserId)
        {
            var user = this.userResolverService.Resolve(chatUserId);

            return this.Ok(user);
        }

        /// <summary>
        /// 获取指定聊天用户的状态。
        /// </summary>
        /// <param name="chatUserId">聊天用户编号。</param>
        /// <response code="200">指定的聊天用户的状态。</response>
        /// <response code="204">指定的聊天用户不存在或已被删除。</response>
        /// <returns></returns>
        [HttpGet("{chat_user_id}/[action]")]
        [ProducesResponseType(typeof(PingDto), 200)]
        public IActionResult Ping([FromRoute(Name = "chat_user_id")] string chatUserId)
        {
            var online = this.webSocketContextService.CheckOnlineAsync(chatUserId).Result;

            return this.Ok(new PingDto
            {
                Status = online ? OnlineStatus.Online : OnlineStatus.Offline
            });
        }

        /// <summary>
        /// 生成匿名用户访问令牌 (适用于聊天相关接口)。
        /// </summary>
        /// <response code="200">匿名用户访问令牌。</response>
        /// <returns></returns>
        [HttpPost("[action]")]
        [ProducesResponseType(typeof(TokenDto), 200)]
        [AllowAnonymous]
        public IActionResult GenerateAnonymousToken()
        {
            var anonymous = userResolverService.GenerateAnonymous();

            return this.Ok(new TokenDto
            {
                Description = anonymous.UserName,
                AccessToken = anonymous.GenerateToken(expiration),
                ExpiresIn = (int)expiration.TotalSeconds,
                EndTime = DateTime.Now.AddSeconds((expiration.TotalSeconds - 12 * 3600))
            });
        }
    }
}
